INSTALL_DIR   = @INSTALL_DIR@

BUILD_VERSION := $(shell @GIT@ describe --tags)

CC            = @CC@

INCLUDE_PCAP  = @PCAP_CFLAGS@

INCLUDE_OTHER = -Ithirdparty \
                @GLIB2_CFLAGS@ \
	        @YARA_CFLAGS@ \
	        @MAXMINDDB_CFLAGS@ \
	        @MAGIC_CFLAGS@ \
	        @CURL_CFLAGS@


LIB_PCAP      = @PCAP_LIBS@
LIB_SNF       = /opt/snf/lib/*.so
LIB_OTHER     = @GLIB2_LIBS@ \
	        @YARA_LIBS@ \
	        @MAXMINDDB_LIBS@ \
	        @CURL_LIBS@ \
                @LIBS@ \
	        thirdparty/http_parser.o \
	        thirdparty/js0n.o \
	        thirdparty/patricia.o \
		@DL_LIB@ -lpthread -lssl -lcrypto -lyaml

C_FILES         = main.c db.c yara.c http.c config.c parsers.c plugins.c field.c trie.c writers.c writer-inplace.c writer-disk.c writer-null.c writer-simple.c readers.c reader-libpcap-file.c reader-libpcap.c reader-tpacketv3.c reader-null.c packet.c session.c rules.c drophash.c
O_FILES         = $(C_FILES:.c=.o)

INSTALL         = @INSTALL@
bindir          = @prefix@/bin
installinclude  = @prefix@/include

SANITIZE_FLAGS  = -DMOLOCH_USE_MALLOC -fno-common -fsanitize=address -fsanitize=integer -fsanitize=nullability
FUZZ_FLAGS      = -DMOLOCH_USE_MALLOC -DFUZZLOCH -fno-common -fsanitize=fuzzer,address -fstack-protector-all -fsanitize-address-use-after-scope

all:thirdparty/js0n.o thirdparty/http_parser.o thirdparty/patricia.o
	$(CC) -fPIC @CFLAGS@ -Wall -Wextra -D_GNU_SOURCE -std=gnu99 -c $(C_FILES) \
	    $(INCLUDE_PCAP) \
	    $(INCLUDE_OTHER) \
	    -DBUILD_VERSION='"$(BUILD_VERSION)"'
	$(CC) -rdynamic -ggdb $(O_FILES) -o moloch-capture \
            @UNDEFINED_FLAGS@ \
	    $(LIB_PCAP) \
	    $(LIB_OTHER) \
	    -lm @RESOLV_LIB@ @MAGIC_LIBS@ -lffi -lz
	(cd parsers; $(MAKE))
	(cd plugins; $(MAKE))

sanitize:thirdparty/js0n.o thirdparty/http_parser.o thirdparty/patricia.o
	$(CC) $(SANITIZE_FLAGS) -fPIC -g -O0 -Wall -Wextra -D_GNU_SOURCE -std=gnu99 -c $(C_FILES) \
	    $(INCLUDE_PCAP) \
	    $(INCLUDE_OTHER) \
	    -DBUILD_VERSION='"$(BUILD_VERSION)"'
	$(CC) $(SANITIZE_FLAGS) -rdynamic -ggdb $(O_FILES) -o moloch-capture \
            @UNDEFINED_FLAGS@ \
	    $(LIB_PCAP) \
	    $(LIB_OTHER) \
	    -lm @RESOLV_LIB@ @MAGIC_LIBS@ -lffi -lz
	-rm */*.so
	(cd parsers; $(MAKE) SANITIZE_FLAGS="$(SANITIZE_FLAGS)")
	(cd plugins; $(MAKE) SANITIZE_FLAGS="$(SANITIZE_FLAGS)")

fuzzloch:thirdparty/js0n.o thirdparty/http_parser.o thirdparty/patricia.o
	$(CC) $(FUZZ_FLAGS) -fPIC -g -O0 -Wall -Wextra -D_GNU_SOURCE -std=gnu99 -c $(C_FILES) \
	    $(INCLUDE_PCAP) \
	    $(INCLUDE_OTHER) \
	    -DBUILD_VERSION='"$(BUILD_VERSION)"'
	$(CC) $(FUZZ_FLAGS) -rdynamic -ggdb $(O_FILES) -o fuzzloch-capture \
            @UNDEFINED_FLAGS@ \
	    $(LIB_PCAP) \
	    $(LIB_OTHER) \
	    -lm -lresolv -lmagic -lffi -lz
	-rm */*.so
	(cd parsers; $(MAKE) SANITIZE_FLAGS="$(FUZZ_FLAGS)")
	(cd plugins; $(MAKE) SANITIZE_FLAGS="$(FUZZ_FLAGS)")

thirdparty/js0n.o:thirdparty/js0n.c
	$(CC) -c thirdparty/js0n.c -o thirdparty/js0n.o

thirdparty/patricia.o:thirdparty/patricia.c
	$(CC) -c thirdparty/patricia.c -o thirdparty/patricia.o

thirdparty/http_parser.o: thirdparty/http_parser.c
	$(CC) -ggdb -DNDEBUG -DHTTP_PARSER_STRICT=0 -DHTTP_PARSER_DEBUG=0 @CFLAGS@ -c thirdparty/http_parser.c -o thirdparty/http_parser.o

install: installdirs
	@mkdir -p $(installinclude)
	$(INSTALL) *.h thirdparty/http_parser.h $(installinclude)
	$(INSTALL) moloch-capture $(bindir)/moloch-capture

installdirs:
	$(INSTALL) -d $(bindir)
	(cd parsers; $(MAKE) install)
	(cd plugins; $(MAKE) install)

check:
	(cd plugins; $(MAKE) check)

distclean realclean clean:
	rm -f *.o moloch-capture */*.o */*.so

cppcheck:
	cppcheck --enable=all --std=c99 -I. -Ithirdparty *.c plugins/*.c parsers/*.c
